﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;

namespace LoomDataEmulator.TestSuite.TestHelpers
{
    public class CmiDaiFileCreatorHelper
    {
        public static string URDU001CMI = "HostId          Karsten\nCommandId       000Urdu\nSource          HOST\nSqlStatement    ODB_IoU_WarpType %WarpType_id,%Color,%WarpTypeComment,%NumberOfEnds,%NumberOfRibbon,%NumberOfSectionBeam,%NumberOfWarp,%NominalLength,%Pass_id,null,null,null,%Active\nEOF";
        public static string URDU001DAI = "\"U110001101\",016711935,\"2354-14/1 CARD 100  CO\",2354,0000,0004,0001,03425.0,\"URDIRE\",1\n\"U210001841\",016711935,\"1896-12/1 PENT 100 ALG\",1896,0000,0004,0001,02000.0,\"URDIRC\",1";

        public static string FIOS001CMI = "HostId          Karsten\nCommandId       001Fios\nSource          HOST\nSqlStatement    ODB_IoU_Yarn %Yarn_id,%YarnDesc,%Color,%EffTex,null,null,null,null,null,%YarnComment,%Supplier_id,%Active\nEOF";
        public static string FIOS001DAI = "\"F110017991\",\"2/34 MERC.GAZEADO 10\",\"0000\",0059.0,\"2/34 CARD.MERC. 100 CO  70.00\",\"COMPRADO\",1\n\"F130017961\",\"14/1 CARD 100  CO\",\"0000\",0042.1,\"14/1 CARD 100 CO  48.00\",\"FABRICADO\",1\n\"F130017961\",\"14/1 CARD 100  CO\",\"0000\",0042.1,\"14/1 CARD 100 CO  48.00\",\"FABRICADO\",1\n\"F130018781\",\"12/1 PENT 100 CO\",\"0000\",0049.2,\"12/1 PENT 100 CO  48.00\",\"FABRICADO\",1";

        static string pathTemp=Convert.ToString(ConfigurationManager.AppSettings.Get("ConsoleInputFolder"));

        public static void WriteTestFiles()
        {
            if (new DirectoryInfo(pathTemp) != null)
            {
                CmiDaiFileCreatorHelper.WriteToFile(pathTemp + "\\URDU001.CMI", CmiDaiFileCreatorHelper.URDU001CMI);
                CmiDaiFileCreatorHelper.WriteToFile(pathTemp + "\\URDU001.DAI", CmiDaiFileCreatorHelper.URDU001DAI);
                CmiDaiFileCreatorHelper.WriteToFile(pathTemp + "\\001Fios.cmi", CmiDaiFileCreatorHelper.FIOS001CMI);
                CmiDaiFileCreatorHelper.WriteToFile(pathTemp + "\\001Fios.dai", CmiDaiFileCreatorHelper.FIOS001DAI);
            }
        }

        public static void WriteToFile(string path, string content)
        {
            var writer = new StreamWriter(path);

            writer.Write(content);
            writer.Flush();
            writer.Close();
            writer = null;
        }

        public static void DeleteTestFiles()
        {
            try
            {
                var fileInfo = new FileInfo(pathTemp + "\\URDU001.CMI");
                fileInfo.Delete();

                fileInfo = new FileInfo(pathTemp + "\\URDU001.DAI");
                fileInfo.Delete();

                fileInfo = new FileInfo(pathTemp + "\\001Fios.cmi");
                fileInfo.Delete();

                fileInfo = new FileInfo(pathTemp + "\\001Fios.dai");
                fileInfo.Delete();
            }
            catch (Exception)
            {
                
                throw;
            }
            
        }

        public static void DeleteTestFiles(string path)
        {
            try
            {
                var fileInfo = new FileInfo(String.Format("{0}\\URDU001.CMI", path));
                fileInfo.Delete();

                fileInfo = new FileInfo(String.Format("{0}\\URDU001.DAI", path));
                fileInfo.Delete();

                fileInfo = new FileInfo(String.Format("{0}\\001Fios.cmi", path));
                fileInfo.Delete();

                fileInfo = new FileInfo(String.Format("{0}\\001Fios.dai", path));
                fileInfo.Delete();
            }
            catch (Exception)
            {
                throw;
            }

        }

        public static void WriteTestFiles(string path)
        {
            if (new DirectoryInfo(path) != null)
            {
                CmiDaiFileCreatorHelper.WriteToFile(String.Format("{0}\\URDU001.CMI", path), CmiDaiFileCreatorHelper.URDU001CMI);
                CmiDaiFileCreatorHelper.WriteToFile(String.Format("{0}\\URDU001.DAI", path), CmiDaiFileCreatorHelper.URDU001DAI);
                CmiDaiFileCreatorHelper.WriteToFile(String.Format("{0}\\001Fios.cmi", path), CmiDaiFileCreatorHelper.FIOS001CMI);
                CmiDaiFileCreatorHelper.WriteToFile(String.Format("{0}\\001Fios.dai", path), CmiDaiFileCreatorHelper.FIOS001DAI);
            }
            else
            {
                throw new Exception("Could not create files");
            }
        }
    }
}
